<?php

class AuthorsController extends AppController {

//     function backend_login() {
//         $this->layout = 'backend_login';
//         if (!empty($this->data)) {
//             $conditions['Author.username'] = $this->data['Author']['username'];
//             $conditions['Author.password'] = md5($this->data['Author']['password']);
//             $adminUser = $this->Author->find('first', array('conditions' => $conditions));
//             if (!empty($adminUser)){
//                 $this->Session->write('Author', $adminUser['Author']);
//                 $this->Session->setFlash('Bienvenido', 'ok');
//                 $this->redirect(array('action' => 'index','backend' => true));
//             }else
//                 $this->Session->setFlash("Usuario o password inválidos.","error");
//         }
//     }

//     function backend_logout() {
//         $this->Session->delete('Author');
//         $this->redirect(array('action' => 'login', 'prefix' => 'backend'));
//     }

    function backend_index(){
        $this->set('authors',$this->Author->find('all'));
    }
    
    function backend_new(){
        $this->__formProccessing();
    }
    
    function backend_edit($id=null){
        $this->__formProccessing($id);
    }
    
    function backend_delete($id){
        if($this->Author->delete($id))
            $this->Session->setFlash('El registro fue eliminado con exito.','ok');
        else
            $this->Session->setFlash('Hubo un error al eliminar el registro.','error');
        $this->redirect(array('action'=>'index'));
    }
    
    private function __formProccessing($id=null){
        if(!empty($this->request->data)){
//     			print "<pre>";
//     			print_r($this->request->data);
//     			print "</pre>";
//     			exit;
            if(!$id)
                $this->Author->create();
            if($this->Author->save($this->request->data)){
                $this->Session->setFlash('El registro fue guardado con exito.','ok');
                $this->redirect(array('action'=>'index'));
            }else{
                $this->Session->setFlash('Hubo un error al procesar el registro.','error');
            }
        }
        if(is_numeric($id))
            $this->request->data = $this->Author->findById($id);
    }
}

?>
